let markId = getUrlString('markid');
let userName;//本人用户id
//获取用户信息
function getsetting () {
    getData('get','/niu/apiv3/member_getsetting.php',{'markid':markId},function (data) {
        userName = 'djuser_'+ data.userid
    })
}
getsetting();


// getnative('dancool://message_getConversationList=1');
var mRefresh = new MiniRefresh({
    container: '#mrefresh', //这里是插件的容器ID
    down: {
        isAuto: false,
        callback: function () {
            // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
            $('#conversationList').empty();
            mRefresh.endDownLoading();
        }
    }
    ,
    up: {
        isAuto: true,
        callback: function () {
            // 向上拉后的回调事件
            getnative('dancool://message_getConversationList=1');
        }
    }
});


function getnative(actionurl) {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isiOS) {
        window.webkit.messageHandlers.getnative.postMessage(actionurl);
    }else {
        ikags.getnative(actionurl)
    }
}
let deleteChatName;
let deleteGroupName;
function jsnativeresult(tag1,tag2,tag3) {

    if(tag1 === 'dancool://message_getConversationList=1'){
        console.log(tag3);
        let text = tag3.replace(/<[^<>]+>/g,"");
        let jsonTag3 = eval('(' + text + ')');
        var  append = '';
        console.log(jsonTag3);
        if (jsonTag3.length === 0){
            $('.nullContent').show();
            $('#mrefresh').hide();
        } else{
            $('.nullContent').hide();
            $(jsonTag3).each(function (index) {
                if(this.type === 'group'){
                    // append += '<li class="group">' +
                    //     '       <input class="name" type="hidden" value="'+this.targetInfo.gid+'">'+
                    //     '                <div class="left">' +
                    //     '                    <img src="../../res/img/messages/headPic.png" alt="">' +
                    //     '                </div>' +
                    //     '                <div class="right">' +
                    //     '                    <p class="username">' +
                    //     '                   <span>'+this.title+'</span>' ;
                    // if(this.unReadMsgCnt !== 0){
                    //     append += '                        <span class="number">'+this.unReadMsgCnt+'</span>';
                    // }
                    //     append +='                        <span class="time">'+getTsFormatDate(this.lastMsgDate)+'</span>' +
                    //     '                    </p>' +
                    //     '                    <p class="userContent">' ;
                    // if (this.latestText === ''){
                    //     append +='                        <span class="messageContent">图片</span>' ;
                    // } else {
                    //     append+='                        <span class="messageContent">'+this.latestText+'</span>' ;
                    // }
                    // append += '                    </p>' +
                    //     '                </div>' +
                    //     '<img class="deleteChat" src="../../res/img/messages/delete.png" alt="">'+
                    //     '       <input class="deleteChatName" type="hidden" value="'+this.targetInfo.gid+'">'+
                    //     '            </li>';
                }else if(this.type === 'single'){
                    append += '<li class="single">' +
                        '       <input class="name" type="hidden" value="'+this.targetInfo.username+'">'+
                        '                <div class="left">' +
                        '                    <img src="'+getHeaderPic(markId,this.targetInfo.username)+'" alt="">' +
                        '                </div>' +
                        '                <div class="right">' +
                        '                    <p class="username">' +
                        '                   <span>'+this.title+'</span>' ;
                    if(this.unReadMsgCnt !== 0){
                        append += '                        <span class="number">'+this.unReadMsgCnt+'</span>';
                    }
                    append+='                        <span class="time">'+getTsFormatDate(this.lastMsgDate)+'</span>' +
                        '                    </p>' +
                        '                    <p class="userContent">' ;
                    if (this.latestText === ''){
                        append +='                        <span class="messageContent">图片</span>' ;
                    } else {
                        append+='                        <span class="messageContent">'+this.latestText+'</span>' ;
                    }
                    append += '                    </p>' +
                        '                </div>' +
                        '<img class="deleteChat" src="../../res/img/messages/delete.png" alt="">'+
                        '       <input class="deleteChatName" type="hidden" value="'+this.targetInfo.username+'">'+
                        '            </li>';
                }

            });
            $('#conversationList').append(append);
            mRefresh.endUpLoading(true);
            mRefresh.scrollTo('5','30');

            //删除会话

            $('#conversationList .single .deleteChat').click(function () {
                deleteChatName = $(this).next('.deleteChatName').val();
                getnative('dancool://message_deleteSingleConversation='+deleteChatName+'')
            });
            //删除群组会话
            $('#conversationList .group .deleteChat').click(function () {
                deleteGroupName = $(this).next('.deleteChatName').val();
                getnative('dancool://message_deleteGroupConversation='+deleteGroupName+'');
            });

            //私聊消息
            $('#conversationList .single .left,#conversationList .single .right').click(function () {
                let toUserName = $(this).prevAll('.name').val();
                getnative('dancool://message_enterSingleConversation='+toUserName+'');
                $(this).find('.right').children('.userContent').find('.number').hide();
                window.location.href = 'dancool://fullweb=https://api.odancool.com/weixinapp/view/messages/sessionList.html?toUserName='+toUserName
            });

            //群聊消息
            $('#conversationList .group .left,#conversationList .group .right').click(function () {
                let toUserName = $(this).prevAll('.name').val();
                getnative('dancool://message_enterGroupConversation='+toUserName+'');
                $(this).find('.right').children('.userContent').find('.number').hide();
                window.location.href = 'dancool://fullweb=https://api.odancool.com/weixinapp/view/messages/group.html?toUserName='+toUserName+'&userName='+userName
            });
        }
    }
    if (tag1 === 'dancool://message_deleteSingleConversation='+deleteChatName+'') {
        laymsg('删除成功');
        setTimeout(function () {
            window.location.reload();
        },500)
    }

    if (tag1 === 'dancool://message_deleteGroupConversation='+deleteGroupName+'') {
        laymsg('删除成功');
        setTimeout(function () {
            window.location.reload();
        },500)
    }
}